package com.milk.milkmedia.utils;

import android.util.Log;
import com.milk.milkmedia.ui.HomePageActivity;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 微安 on 2015/8/22.
 */
public class LrcHandle {
    private List<Map<String,Object>> mLyrics = new ArrayList<Map<String, Object>>();

    public boolean readLRC(String path) {
        Map<String,Object> lyric = null;
        if (path.length() == 0||path.equals("null")){
            return false;
        }else {
            try {
                File file = new File(path);
                String code = getLyricCharset(file);
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(
                        fileInputStream, code);
                BufferedReader bufferedReader = new BufferedReader(
                        inputStreamReader);
                String s = "";
                while ((s = bufferedReader.readLine()) != null && s.length() != 0) {
                    lyric = new HashMap<String, Object>();
                    addTimeToList(s, lyric);
                    if ((s.indexOf("[ar:") != -1) || (s.indexOf("[ti:") != -1)
                            || (s.indexOf("[by:") != -1)) {
                        s = s.substring(s.indexOf(":") + 1, s.indexOf("]"));
                    } else {
                        if (s.length() != 0) {
                            String ss = s.substring(s.indexOf("["), s.indexOf("]") + 1);
                            s = s.replace(ss, "");
                        }
                    }
                    lyric.put("word", s);
                    mLyrics.add(lyric);
                }
                bufferedReader.close();
                inputStreamReader.close();
                fileInputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Log.e(HomePageActivity.Tag, "没有歌词文件，赶紧去下载");
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(HomePageActivity.Tag, "没有读取到歌词");
                return false;
            }

        }
        return true;
    }

    private int timeHandler(String string) {
        string = string.replace(".", ":");
        String timeData[] = string.split(":");
        int minute = Integer.parseInt(timeData[0]);
        int second = Integer.parseInt(timeData[1]);
        int millisecond = Integer.parseInt(timeData[2]);
        int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
        return currentTime;
    }

    private void addTimeToList(String string, Map<String,Object> lyric ) {
        Matcher matcher = Pattern.compile(
                "\\[\\d{1,2}:\\d{1,2}([\\.:]\\d{1,2})?\\]").matcher(string);
        if (matcher.find()) {
            String str = matcher.group();
            lyric.put("time", timeHandler(str.substring(1,
                    str.length() - 1)));
        }
    }

    public String getLyricCharset(File file) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            Log.e(HomePageActivity.Tag, "未读取到编码格式的文件");
        }
        BufferedInputStream bin = new BufferedInputStream(
                fileInputStream);
        int p = 0;
        try {
            p = (bin.read() << 8) + bin.read();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(HomePageActivity.Tag, "格式得到编码格式读文件有误");
        }
        String code = null;
        switch (p) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }
        return code;
    }

    public List<Map<String, Object>> getmLyrics() {
        return mLyrics;
    }

    public void setmLyrics(List<Map<String, Object>> mLyrics) {
        this.mLyrics = mLyrics;
    }
}
